package com.infinitus.multids.task;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.infinitus.multids.domain.CatConfigInfo;
import com.infinitus.multids.domain.primary.ContentInfo;
import com.infinitus.multids.domain.secondary.ProductInfo;
import com.infinitus.multids.service.RedisService;

@Component
public class FetchCatDataTask {

	private static final Logger LOG = LoggerFactory.getLogger(FetchCatDataTask.class);

	@Autowired
	private RedisService redisService;

	@Autowired
	private CatConfigInfo catConfigInfo;

	@Autowired
	@Qualifier("entityManagerFactoryPrimary")
	private EntityManagerFactory entityManagerFactoryPrimary;

	@Autowired
	@Qualifier("entityManagerFactorySecondary")
	private EntityManagerFactory entityManagerFactorySecondary;


	public Map<String, String> getContent(String id) {
		Map<String, String> resMap =  new HashMap<String, String>();
		EntityManager entityManagerPrimary = entityManagerFactoryPrimary.createEntityManager();
		String sql = "select t1.title, "
				+ "(select t2.name from cmstop_category_tmp t2 where t1.catid = t2.catid), "
				+ "(select t3.name from cmstop_category t3 where t1.catid = t3.catid) "
				+ "from cmstop_content t1 where t1.contentid=:id and  t1.modelid='100' ";
		List<Object[]> cis = entityManagerPrimary.createNativeQuery(sql).setParameter("id", id).getResultList();
		if(cis!=null && cis.size()>0){
			Object[] ci = cis.get(0);
			if (ci != null && ci.length > 2) {
				redisService.set(ContentInfo.TITLE + id, ci[0]);
				if (ci[1] != null && !"".equals(ci[1])) {
					redisService.set(ContentInfo.NAME +  id, ci[1]);
				} else {
					redisService.set(ContentInfo.NAME +  id, ci[2]);
				}
				resMap.put("status", "success");
			}
		}else{
			resMap.put("status", "error");
			resMap.put("reson", "cannot found data");
		}
		entityManagerPrimary.clear();
		entityManagerPrimary.close();
		return resMap;
	}
	
	@Scheduled(cron = "0 30 0/4 * * ? ")
	public void loadContentInfo() {
		EntityManager entityManagerPrimary = entityManagerFactoryPrimary.createEntityManager();
		String sql = "select t1.contentid,t1.title, "
				+ "(select t2.name from cmstop_category_tmp t2 where t1.catid = t2.catid), "
				+ "(select t3.name from cmstop_category t3 where t1.catid = t3.catid) "
				+ "from cmstop_content t1 where t1.modelid='100' ";
		List<Object[]> cis = entityManagerPrimary.createNativeQuery(sql).getResultList();
		for (Object[] ci : cis) {
			if (ci != null && ci.length > 2) {
				redisService.set(ContentInfo.TITLE + ci[0], ci[1]);
				if (ci[2] != null && !"".equals(ci[2])) {
					redisService.set(ContentInfo.NAME + ci[0], ci[2]);
				} else {
					redisService.set(ContentInfo.NAME + ci[0], ci[3]);
				}
			}
		}
		entityManagerPrimary.clear();
		entityManagerPrimary.close();
	}
	
	public Map<String, String> getProduct(String id) {
		Map<String, String> resMap =  new HashMap<String, String>();
		EntityManager entityManagerSecondary = entityManagerFactorySecondary.createEntityManager();
		String sql = "SELECT PRODUCT_FULL_NAME  FROM sdm_product_info where PRODUCT_CODE = :id";
		List<String> nameRes = entityManagerSecondary.createNativeQuery(sql).setParameter("id", id).getResultList();
		if(nameRes!=null && nameRes.size()>0){
			redisService.set(ProductInfo.TITLE + id, nameRes.get(0));
			resMap.put("status", "success");
		}else{
			resMap.put("status", "error");
			resMap.put("reson", "has not data");
		}
		entityManagerSecondary.clear();
		entityManagerSecondary.close();
		return resMap;
	}
	
	@Scheduled(cron = "0 0 * * * ? ")
	public void loadProductInfo() {
		EntityManager entityManagerSecondary = entityManagerFactorySecondary.createEntityManager();
		String sql = "SELECT DISTINCT PRODUCT_CODE, PRODUCT_FULL_NAME  FROM sdm_product_info";
		List<Object[]> pis = entityManagerSecondary.createNativeQuery(sql).getResultList();
		for (Object[] pi : pis) {
			if (pi != null && pi.length > 1) {
				redisService.set(ProductInfo.TITLE + pi[0], pi[1]);
			}
		}
		entityManagerSecondary.clear();
		entityManagerSecondary.close();
	}

	

}
